import os
import time
from common.base import Base, get_driver
from selenium.webdriver.common.by import By


class IndexPage(Base):
    def to_left_menu(self):
        """切换到左侧边框"""
        self.choice_frame((By.XPATH, '//frame[@id="menu-frame"]'))

    # TODO 添加商品相关方法
    def to_order_list(self):
        """来到订单列表页面"""
        self.click((By.XPATH, '//li[@data-key="01_goods_list"]'))
        time.sleep(2)
        self.click((By.LINK_TEXT, '订单列表'))

    def to_goods_manage(self):
        """点击到添加新商品"""
        self.click((By.XPATH, '//li[@data-key="01_goods_list"]'))
        time.sleep(2)
        self.click((By.LINK_TEXT, '添加新商品'))

    def choice_words_type(self):
        """选择文字样式"""
        self.click((By.XPATH, "//option[@value='strong']"))

    def add_brand(self, content):
        """添加品牌"""
        self.click((By.XPATH, "//button[@onclick='rapidBrandAdd()']"))
        time.sleep(1)
        self.send_keys((By.NAME, "addedBrandName"), content)
        self.click((By.XPATH, "//button[@onclick='addBrand()']"))

    def to_main_menu(self):
        """切换到详情页"""
        self.choice_parent_frame()
        self.choice_frame((By.XPATH, '//frame[@id="main-frame"]'))

    def input_name(self, goods_name):
        """输入名称"""
        self.send_keys((By.NAME, "goods_name"), goods_name)

    def choice_type(self):
        """选择商品类型"""
        self.click((By.XPATH, "//select/option[@value='16']"))

    def input_price(self, goods_price):
        """输入售价"""
        self.send_keys((By.NAME, "shop_price"), goods_price)

    def update_picture(self, filename):
        """上传商品图片"""
        common_path = os.path.dirname(__file__)
        project_path = os.path.dirname(common_path)

        pic_url = os.path.join(project_path, 'data', filename)
        self.send_keys((By.NAME, "goods_img"), pic_url)

    def make_sure(self):
        """点击确定"""
        self.click((By.XPATH, "//input[@value=' 确定 ']"))

    # TODO 生成订单查看相关代码

    def to_add_order(self):
        """点击到添加新商品"""
        self.click((By.XPATH, '//li[@data-key="02_order_list"]'))
        time.sleep(2)
        self.click((By.LINK_TEXT, '添加订单'))

    def to_order_list(self):
        """来到订单列表页面"""
        self.click((By.XPATH, '//li[@data-key="02_order_list"]'))
        time.sleep(2)
        self.click((By.LINK_TEXT, '订单列表'))

    def next_step(self):
        """点击下一步"""
        self.click((By.XPATH,"//input[@value='下一步']"))
        time.sleep(3)

    def search_goods_number(self,goods_number):
        """通过商品编号查找商品"""
        self.send_keys((By.NAME,"keyword"),goods_number)
        self.click((By.NAME,"search"))
        time.sleep(4)

    def add_to_order(self):
        """将查找到的商品添加到订单"""
        self.click((By.NAME, "add_goods"))
        time.sleep(4)

    def order_consignee(self,consignee):
        """输入收货人"""
        self.send_keys((By.NAME, "consignee"), consignee)

    def choice_addr(self):
        """选择收货地址"""
        self.click((By.XPATH,"//option[@value='1']"))
        time.sleep(2)
        self.click((By.XPATH,"//option[@value='24']"))
        time.sleep(2)
        self.click((By.XPATH, "//option[@value='271']"))
        time.sleep(2)
        self.click((By.XPATH, "//option[@value='2713']"))

    def input_email(self,email):
        """输入邮件地址"""
        self.send_keys((By.NAME, "email"), email)

    def input_addr(self,addr):
        """输入详细地址"""
        self.send_keys((By.NAME, "address"), addr)

    def input_tel(self,tel):
        """输入电话号码"""
        self.send_keys((By.NAME, "mobile"), tel)

    def choice_courier(self):
        """点选快递方式"""
        self.click((By.XPATH, "//input[@value='5']"))

    def choice_pay(self):
        """选择付款方式"""
        self.click((By.XPATH, "//input[@value='2']"))

    def finish_order(self):
        """点击完成订单"""
        self.click((By.XPATH, "//input[@value='完成']"))

    def get_order_number(self):
        """获取订单号"""
        return self.get_text((By.XPATH, "//td[@width='34%']"))

    def get_real_order_number(self):
        """获取生成的订单的订单号"""
        return self.get_text((By.ID, "order_0"))

if __name__ == '__main__':
    driver = get_driver()
    driver.get('http://localhost:8080/ecshop/admin/index.php')
    test = IndexPage(driver)
    test.click((By.XPATH, '//span[@onclick]'))
    test.click((By.ID, 'cloudLogin'))
    test.send_keys((By.NAME, 'username'), 'root1234')
    test.send_keys((By.NAME, 'password'), 'root1234')
    test.click((By.XPATH, '//input[@type="submit"]'))
    """
    print(test.driver.title)
    test.to_left_menu()
    test.to_goods_manage()
    test.to_main_menu()
    test.input_name("七匹狼超人同款内裤")

    test.choice_words_type()
    test.add_brand("七匹狼")
    test.update_picture("super_panties.jpg")

    test.choice_type()
    test.input_price("19.9")
    test.make_sure()
    """
    test.to_left_menu()
    test.to_add_order()
    test.to_main_menu()
    test.next_step()
    test.search_goods_number("70")
    test.add_to_order()
    test.next_step()
    test.order_consignee("yun")
    test.choice_addr()
    test.input_email("12.@123.com")
    test.input_addr("成都高新")
    test.input_tel("12312342234")
    test.next_step()
    test.choice_courier()
    test.next_step()
    test.choice_pay()
    test.next_step()
    test.next_step()
    test.finish_order()
    print(test.get_order_number())

    test.choice_parent_frame()
    test.to_left_menu()
    test.to_order_list()
    test.to_main_menu()
    print(test.get_real_order_number())